<?php


class output_manager() {
    
    /**
     * Outputs the app <head>
     */
    public function displayHead() {
    
        global $CONFIG;
    
        // Title = metadescription ~= metakeywords
        $title = $CONFIG->titleprefix;
        $metadescription = $title = $CONFIG->titleprefix;
        
        if (!empty($_SESSION['labels'])) {
            
            // Decode before output
            $labelsarray = decode_text($_SESSION['labels']);
            
            $title .= ' - '.implode(' ', $labelsarray);
            $metadescription = $metadescription.' - '.implode(' ', $labelsarray);
            $metakeywords = $title;
        }
    
        // Compressed version
        if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
            ob_start("ob_gzhandler");
        }  else {
            ob_start();
        }
        
        $output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.LB;
        $output.= '<html xmlns="http://www.w3.org/1999/xhtml">'.LB;
        
        $output.= '<head>'.LB;
        $output.= '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'.LB;
        $output.= '<meta name="description" content="'.$metadescription.'"/>'.LB;
        $output.= '<meta name="keywords" content="'.$metakeywords.'"/>'.LB;
        $output.= '<title>'.$title.'</title>'.LB;
        
        // Styles
        $output.= '<link rel="stylesheet" type="text/css" href="'.$CONFIG->webdir.'/style.css" />'.LB;
        $output.= '<link rel="shortcut icon" href="'.$CONFIG->webdir.'/favicon.ico" />';
        
        // Analytics
        $output.= $CONFIG->googleanalyticscode.LB;
        
        $output.= '</head>'.LB.LB;
    
        echo $output;
    }
    
    
    /**
     * Display the logo image
     */
    public function showLogo() {
        global $CONFIG;
        echo '<a id="logo" href="'.$CONFIG->webdir.'/'.$_SESSION['lang'].'">'.$CONFIG->logotext.'</a>'.LB;
        echo '<img src="'.$CONFIG->webdir.'/beta.png" class="beta"/>';
    }
    
    
    /**
     * Displays the page footer
     */
    public function displayFoot() {
    
        global $CONFIG;
        
        echo '<br/><br/>';
        echo '<span class="footer">'.LB;
        echo '<a href="mailto:'.$CONFIG->email.'?subject='.System::$S['addblog'].'">'.System::$S['addblog'].'</a>';
        echo '&nbsp;&nbsp;2011 ';
        echo '&nbsp;'.$CONFIG->email;
        echo '&nbsp;&nbsp;<a href="mailto:'.$CONFIG->email.'?subject='.System::$S['removeblog'].'">'.System::$S['removeblog'].'</a>';
        
        echo '</span>'.LB;
        
        echo '<br/><br/><a href="http://validator.w3.org/check?uri=referer" target="_blank"><img
            src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" class="xhtmlvalidator"/></a>';
    }
    
    
    /**
     * Displays the langs avaiable list with links
     */
    public function showLangsLinks() {
    
        global $CONFIG, $STRING;
    
        $output = '<div id="langslinks">';
    
        foreach ($CONFIG->availablelanguages as $lang) {
    
            if ($_SESSION['lang'] == $lang) {
                $langsstring[] = System::$S[$lang];
            } else {
                $langsstring[] = '<a href="'.$CONFIG->webdir.'/'.$lang.'">'.System::$S[$lang].'</a>';
            }
        }
    
        $output.= implode(", ", $langsstring);
    
        $output.= '</div>';
    
        echo $output;
    }
    
    
    /**
     * Outputs the labels cloud
     * 
     * @param array $keywordsarray
     */
    public function showLabelsCloud($keywordsarray) {
    
        global $CONFIG;
        
        // Remove the session labels
        if (!empty($_SESSION['labels'])) {
            foreach ($_SESSION['labels'] as $label) {
                if (!empty($keywordsarray[$label])) {
                    unset($keywordsarray[$label]);
                }
            }
        }
        
        if (empty($keywordsarray)) {
            return false;
        }
        
        $max = max($keywordsarray);
        $min = min($keywordsarray);
        $difference = $max - $min;
    
        // If all the tags has the same weight
        if ($difference == 0) {
            $difference = 1;
        }
        
        // Every $break lines a line break
        $break = 6;
        $i = 1;
        
        $output = '';
    
        
        foreach ($keywordsarray as $keywordname => $appearances) {
    
           $relative = round(($CONFIG->style_cloudmaxfont * ($appearances - $min)) / $difference);
    
           $output.= '<span class="keywordsize_'.$relative.'">';
           
           $q = '';
           if (!empty($_SESSION['labels'])) {
               $q .= implode('-', decode_text($_SESSION['labels'])).'-';
           }
           $q .= decode_text($keywordname);
           
           $output.= '<a href="'.$this->link_output($q).'">'.decode_text($keywordname).'</a>';
           $output.= "</span> ";
           
           if ($i % $break == 0) {
               $output.= '<br/>';
           }
           
           $i++;
        }
        
        if ($output != '') {
            echo '<div class="keywordsdiv">'.LB;
            echo $output.LB;
            echo '</div>'.LB;
        }
        
    }
    
    
    /**
     * Outputs the navigation bar
     */
    public function showBreadcrumb() {
        
        global $CONFIG;
        
        if (!empty($_SESSION['labels'])) {
            
            $lastselected = count($_SESSION['labels']) - 1;
            
            echo '<div class="breadcrumb">'.LB;
            
            // First one = home
            $entries = array('<h1><a href="'.$CONFIG->webdir.'/'.$_SESSION['lang'].'">'.System::$S['home'].'</a></h1>');
            
            // To store the previous tags
            $prevs = array();
            
            $key = 0;
            foreach ($_SESSION['labels'] as $label) {
                
                $label = decode_text($label);
                
                if ($lastselected != $key) {
                    
                    // A concatenation of the previous labels maintaining the order
                    $q = '';
                    if (!empty($prevs)) {
                        $q .= implode('-', $prevs).'-';
                    }
                    $q .= $label;
                    
                    // Add the label to the navigation
                    $prevs[] = $label;
                    
                    $label = '<a href="'.$this->link_output($q).'" title="'.$label.'">'.$label.'</a>';
                    
                }
                
                $entries[] = '<h1>'.$label.'</h1>';
                $key++;
            }
            
            echo implode(' > ', $entries);
            
            echo '</div>'.LB;
        }
    }
    
    
    /**
     * Displays the entries list
     *
     * @param array $entries
     */
    public function showEntries($entries) {
    
        global $CONFIG;
        
        if (!empty($entries)) {
            
            echo '<table id="entries"><tr>'.LB;
            foreach ($entries as $i => $entry) {
                
                if ($i != 0 && $i % $CONFIG->entriesperrow == 0) {
                    echo '</tr><tr>'.LB;
                }
                $this->showEntry($entry, $i);
            }
            echo '</tr></table>'.LB;
        }
    }
    
    
    /**
     * Displays a single entry
     *
     * @param object $entry
     * @param integer $i The col num
     */
    private function showEntry($entry, $i) {
    
        global $CONFIG;
        
        $classname = 'entrybox';
    
        echo '<td>'.LB;
        echo '<h2><a href="'.$entry->url.'" target="_blank" title="'.$entry->title.'">'.$entry->title.'</a></h2><br/>';
        
        // Only the first $CONFIG->wordsperentry ~words
        echo $this->entrytext_output($entry);
        echo '</td>'.LB;
    
    }
    
    
    private function entrytext_output($entry) {
    
        global $CONFIG;
        
        $textarray = explode(' ', $entry->contenttoshow);
    
        if (count($textarray) < $CONFIG->wordsperentry) {
            return implode(' ', $textarray);
        }
        
        $output = implode(' ', array_slice($textarray, 0, $CONFIG->wordsperentry)).' ... ';
        $output.= '<a href="'.$entry->url.'" target="_blank">'.System::$S['readmore'].'</a>';
        
        return $output;
    }
    
    
    /**
     * Returns a link based on $q param and the session language
     * @param array $q
     * @param boolean $returnextension Return the .html or not
     */
    public function link_output($q, $returnextension = true) {
        global $CONFIG;
        
        $url = $CONFIG->webdir.'/'.$_SESSION['lang'].'/'.str_replace('"', "&quot;", $q);
        
        if ($returnextension) {
            return $url.'.html';
        }
        
        return $url;
    }
    
    
    public function showPagination() {
    
        global $CONFIG;
    
        $searchurl = $this->link_output($_SESSION['q'], false);
        
        $npages = ceil($_SESSION['total'] / $CONFIG->entriesperpage);
    
        if ($npages > 1) {
            for ($i = 1; $i <= $npages; $i++) {
                if ($i == $_SESSION['page']) {
                    $array[] = $i;
                } else {
                    $array[] = '<a href="'.$searchurl.'-page'.$i.'.html">'.$i.'</a>';
                }
            }
    
            echo '<div class="pageslinks">';
            echo implode(' - ', $array);
            echo '</div>'.LB;
    
        } else {
            return false;
        }
    }
    
    
    public function startDisplayContent() {
        echo '<body>';
        echo '<div id="generallayout">'.LB;
    }
    
    
    public function endDisplayContent() {
        echo '</div>'.LB;
        echo '</body>'.LB;
        echo '</html>';
    }
    
}
